package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.botp.BFTrackerServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/*
 * 资金计划调整审核后修改资金计划中剩余银行存款/剩余承兑汇票/剩余信用证
 */
public class fundAdjustAuditOp extends AbstractOperationServicePlugIn {
    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();//审核通过的单据
        for (DynamicObject dataEntity : dataEntities) {
            Map<String, HashSet<Long>> srcBills = BFTrackerServiceHelper.findSourceBills("qeug_fundadjust", new Long[]{dataEntity.getLong("id")});//资金计划调整寻找上游单据
            HashSet<Long> qeug_fundplanids = srcBills.get("qeug_fundplan");//资金计划标识获取对应id
            if (qeug_fundplanids == null || qeug_fundplanids.size() == 0){
                throw new RuntimeException("未查询到上游资金计划单据！");
            }
            for (Long fundplanid : qeug_fundplanids) {
                DynamicObject qeug_fundplan = BusinessDataServiceHelper.loadSingle(fundplanid, "qeug_fundplan");//资金计划
                List<DynamicObject> orderList = new ArrayList<>();
                DynamicObject qeug_fundadjust = BusinessDataServiceHelper.loadSingle(dataEntity.getLong("id"), "qeug_fundadjust");//资金计划调整
                DynamicObjectCollection entryEntityCollection = qeug_fundadjust.getDynamicObjectCollection("entryentity");//资金计划调整分录
                for (DynamicObject entryEntity : entryEntityCollection) {
                    Object originEntryId = entryEntity.getString("qeug_sourceenid").toString();//上游分录ID
                    DynamicObjectCollection entryEntityCollection1 = qeug_fundplan.getDynamicObjectCollection("entryentity");//资金计划分录
                    for (DynamicObject entryEntity1 : entryEntityCollection1) {
                        Object sourceEntryId = entryEntity1.get("id").toString();//资金计划分录ID
                        if (originEntryId.equals(sourceEntryId)) {
                            BigDecimal qeug_newbankamount = entryEntity.getBigDecimal("qeug_newbankamount");//调整后银行存款
                            BigDecimal qeug_ysbankamount = entryEntity1.getBigDecimal("qeug_ysbankamount");//初始银行存款
                            BigDecimal qeug_bankamount = entryEntity1.getBigDecimal("qeug_bankamount");//剩余银行存款
                            BigDecimal newbankamount = qeug_newbankamount.subtract(qeug_ysbankamount).add(qeug_bankamount);
                            entryEntity1.set("qeug_bankamount", newbankamount);//修改剩余银行存款:调整后银行存款-初始银行存款+剩余银行存款
                            BigDecimal qeug_newyshpamount = entryEntity.getBigDecimal("qeug_newyshpamount");//调整后承兑汇票
                            BigDecimal qeug_yshpamount = entryEntity1.getBigDecimal("qeug_yshpamount");//初始承兑汇票
                            BigDecimal qeug_hpamount = entryEntity1.getBigDecimal("qeug_hpamount");//剩余承兑汇票
                            BigDecimal newhpamount = qeug_newyshpamount.subtract(qeug_yshpamount).add(qeug_hpamount);
                            entryEntity1.set("qeug_hpamount", newhpamount);//修改剩余承兑汇票:调整后承兑汇票-初始承兑汇票+剩余承兑汇票
                            BigDecimal qeug_newxyzamount = entryEntity.getBigDecimal("qeug_newxyzamount");//调整后信用证
                            BigDecimal qeug_ysxyzamount = entryEntity1.getBigDecimal("qeug_ysxyzamount");//初始信用证
                            BigDecimal qeug_xyzamount = entryEntity1.getBigDecimal("qeug_xyzamount");//剩余信用证
                            BigDecimal newxyzamount = qeug_newxyzamount.subtract(qeug_ysxyzamount).add(qeug_xyzamount);
                            entryEntity1.set("qeug_xyzamount", newxyzamount);//修改剩余信用证:调整后信用证-初始信用证+剩余信用证
                            orderList.add(qeug_fundplan);
                            SaveServiceHelper.save(orderList.toArray(new DynamicObject[0]));
                        }
                    }
                }
            }
        }
    }
}
